package com.scut.edu.wanyou.home;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.scut.edu.wanyou.Constans;
import com.scut.edu.wanyou.R;
import com.scut.edu.wanyou.TempValue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;


/**
 *
 * @author geniuseoe2012
 *  更多精彩，请关注我的CSDN博客http://blog.csdn.net/geniuseoe2012
 *  android开发交流群：200102476
 */
public class ChatActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */

    private Button mBtnSend;
    private Button mBtnBack;
    private EditText mEditTextContent;
    private ListView mListView;
    private ChatMsgViewAdapter mAdapter;
    private List<ChatMsgEntity> mDataArrays ;
    private ChildHolder ToHolder;
    private TextView titleTextView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_main);
        ToHolder = (ChildHolder)TempValue.VALUES.get("ToHolder");
        mDataArrays = ToHolder.msgs;
        initView();

        initData();
        ToHolder.chat.initChatListener(mAdapter);
    }


    public void initView()
    {
        titleTextView =(TextView)findViewById(R.id.chat_title);
        titleTextView.setText("与"+ToHolder.Id+"聊天中");
        mListView = (ListView) findViewById(R.id.chat_listview);
        boolean t = (mListView == null);
        Log.i("",""+t);
        mBtnSend = (Button) findViewById(R.id.chat_btn_send);
        mBtnSend.setOnClickListener(this);
        mBtnBack = (Button) findViewById(R.id.chat_btn_back);
        mBtnBack.setOnClickListener(this);

        mEditTextContent = (EditText) findViewById(R.id.chat_et_sendmessage);
    }

    public void initData()
    {

        int COUNT = mDataArrays.size();
        for(int i = 0; i < COUNT; i++)
        {
            //一条消息记录
            ChatMsgEntity entity = new ChatMsgEntity();
            entity.setDate(mDataArrays.get(i).getDate());
            if (mDataArrays.get(i).getMsgType())
            {
                entity.setName(ToHolder.Id);
                entity.setMsgType(true);
            }else{
                entity.setName(Constans.USERNAME);
                entity.setMsgType(false);
            }

            entity.setText(ToHolder.msgs.get(i).getText());
            //mDataArrays.add(entity);

        }

        mAdapter = new ChatMsgViewAdapter(this, mDataArrays,null,ToHolder.userHead);
        mListView.setAdapter(mAdapter);

    }


    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
            case R.id.chat_btn_send:
                send();
                break;
            case R.id.chat_btn_back:
                Intent intent = new Intent(ChatActivity.this, HomeActivity.class);

                //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置
                //intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

                //finish();
                break;
        }
    }

    private void send()
    {
        String contString = mEditTextContent.getText().toString();
        if (contString.length() > 0)
        {
            ToHolder.chat.SendMsg(contString);
            ChatMsgEntity entity = new ChatMsgEntity();
            entity.setDate(getDate());
            entity.setName(Constans.USERNAME);
            entity.setMsgType(false);
            entity.setText(contString);
            mDataArrays.add(entity);
            mAdapter.notifyDataSetChanged();

            mEditTextContent.setText("");

            mListView.setSelection(mListView.getCount() - 1);
        }
    }







    private String getDate() {
        Calendar c = Calendar.getInstance();

        String year = String.valueOf(c.get(Calendar.YEAR));
        String month = String.valueOf(c.get(Calendar.MONTH));
        String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
        String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
        String mins = String.valueOf(c.get(Calendar.MINUTE));


        StringBuffer sbBuffer = new StringBuffer();
        sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":" + mins);


        return sbBuffer.toString();
    }
}